<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Activity Report</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="./EldercareMasterPage/_finalstyle.css" rel="stylesheet" />
	<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="./css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
</head>
<?php
	define('CONFIG_FILE',true);
	include './config.php';
	define('DB_FILE',true);
	
	session_start();
	if (empty($_SESSION["username"])||empty($_SESSION["userType"])) {
		Header("Location: ./Index.php");
		exit();
	}else{
		$userType = $_SESSION["userType"];
	}
	
	// set timeout period in seconds
	$inactive = 7200;
	// check to see if $_SESSION['timeout'] is set
	if (isset($_SESSION["timeout"])) {
		$session_life = time() - $_SESSION["timeout"];
		if ($session_life > $inactive) {
			session_destroy();
			Header("Location: Timeout.php");
		}
	}
	$_SESSION["timeout"] = time();
?>
<body>
    <div class="banner">
	</div>
    <div id="main">
		<div class="left-wrapper">
		<div class="logo">
			<img src="images/eldercare.gif" alt="logo" width="240px" />
		</div>
		<div class="menu-wrapper">
			<div id="cssmenu">        
					<?php
					if ($userType == "Therapy Assistant") {
						include_once('./Sidebar/_SidebarTA.php');
					} else if ($userType == "Centre Manager") {
						include_once('./Sidebar/_SidebarCM.php');
					} else {
						include_once('./Sidebar/_SidebarSA.php');
					}
					?>                     
			</div> 
			<div class="clear"></div></div>
		</div>
		<div id="content">
			<div class="content2">
				<b style="float:right; margin: 0px 0 0 0">  
				<?php
					if (!empty($_SESSION["sent"]) && ($_SESSION["sent"] == true)) {

						echo "<script> email();</script>";
						$_SESSION["sent"] = false;
					}
				?>
				<?php
						if (!empty($_SESSION["centreName"])) {
							$Username = $_SESSION["username"];
							$centreName = $_SESSION["centreName"];
							echo "Welcome, " . $Username . " ! " . $centreName . " Branch";
						} else {
							$Username = $_SESSION["username"];
							echo "Welcome, " . $Username;
						}
					?>
					<input type="submit" class="btn btn-danger" value="Logout" onclick="parent.location = 'logout.php'">
				</b>
				</p>
				<br>
				<div style="float:center">
				<br>		
				<h3>Activity Reports</h3>
				<br>
				<form name="reportdates" method="POST" class="form-horizontal">
					<div class="control-group">
						<b>From:</b>
						<div class="controls input-append date form_date3" data-link-field="dtp_input3">
							<input size="16" type="text" value="" readonly style="width:73px;">
							<span class="add-on"><i class="icon-remove"></i></span>
							<span class="add-on"><i class="icon-th"></i></span>
						</div>
						<input type="hidden" name="startDate" id="dtp_input3" value="" />
						<b>To:</b>
						<div name="endDate" class="controls input-append date form_date4" data-link-field="dtp_input4">
							<input  size="16" type="text" value="" readonly style="width:73px;">
							<span class="add-on"><i class="icon-remove"></i></span>
							<span class="add-on"><i class="icon-th"></i></span>
						</div>
						<input type="hidden" name="endDate" id="dtp_input4" value="" />
						<?php
							require_once './Tool/DB/SqlDBManager.class.php';
							if ($userType == "System Admin") {
								echo"<b> &nbsp; Center: </b> <select id='ddlCentre' style=\"width:110px;\" name='ddlCentre'> 
								<option value='0'selected>All</option>";
								
								$sqlDBManager = new SqlDBManager();
								$sql="Select CentreId, CentreName FROM centre";
								$res=$sqlDBManager->queryRows($sql);
								if(!empty($res)){
									for($i=0;$i<count($res);$i++){
										echo "<option value='".$res[$i]['CentreId']."'>".$res[$i]['CentreName']."</option>";
									}
								}									
								echo"</select>";
							}
						?>
						<b>Activity:</b>
						<?php
							require_once './Tool/DB/SqlDBManager.class.php';
							$sqlDBManager = new SqlDBManager();
							if($userType == "System Admin"){
								echo "<select id=\"activity\" name=\"activity\" style=\"width:110px;\" onmouseover=\"showActivity()\">";
								echo "<option value=\"default\" disabled selected>--None--</option>";
								echo"</select>";
							}else{
								echo "<select id=\"activity\" name=\"activity\">";
								$sql="Select ActivityId, ActivityName FROM activity Where CentreName=?";
								$parameters=array($centreName);
								$res=$sqlDBManager->queryRows($sql,$parameters);
								if(!empty($res)){
									for($i=0;$i<count($res);$i++){
										echo "<option value=\"".$res[$i]['ActivityId']."\">".$res[$i]['ActivityName']."</option>";
									}
								}
								echo"</select>";
							}								
						?>
						<b>Sort By:</b>
						<select id="sortBy" name="sortBy" style="width:110px;">
							<option value="NRIC" selected>NRIC</option>
							<option value="CustomerName">Customer Name</option>
							<?php
								if ($userType == "System Admin") {
									echo "<option value='CentreId'>Centre Name</option>";
								}
							?>
							<option value="Daycare">Daycare</option>
						</select>
						&nbsp; <input type="submit" button class="btn"id="submit" value ='Submit'name="submit"/>
					</div>
				</form>
				</p>
				<p></p>
				<p>
				 <?php
				 	require_once './Tool/DB/SqlDBManager.class.php';
					if (isset($_POST['submit'])) {
						if (!empty($_POST['startDate'])&&!empty($_POST['endDate'])) {
							if (!empty($_POST['activity'])) {
									$ActivityId=$_POST['activity'];
									$sqlDBManager = new SqlDBManager();
									$sql = "Select ActivityName from activity where ActivityId=?";
									$parameters=array($ActivityId);
									$res=$sqlDBManager->queryRow($sql,$parameters);
									if(!empty($res)){
										$ActivityName = $res['ActivityName'];
									}	
									echo "<h3>$ActivityName</h3>";
									include_once('./Reports/activitytable.php');
							} else {
								echo "<font color=red>Please select one activity!</font>";
							}
						} else {
							echo "<font color=red>Please pick two dates!</font>";
						}
					}	
                ?>
			</div>
		</div>
	</div>
	</div>
<div id="footer">&copy; 2013 MP22 IIT-IT Temasek Polytechnic</div>
</body>
<script type="text/javascript" src="./js/jquery-1.8.3.min.js" charset="UTF-8"></script>
<script type="text/javascript" src="./js/bootstrap.min.js"></script>
<script type="text/javascript" src="./js/bootstrap-datetimepicker.js" charset="UTF-8"></script>
<script type="text/javascript">
	$('.form_date3').datetimepicker({
		format:"dd/mm/yyyy",
        weekStart: 0,
        todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		minView: 2,
		forceParse: 0,
		pickerPosition:'bottom-left',
		daysOfWeekDisabled: "0,6",
    });
	$('.form_date4').datetimepicker({
		format:"dd/mm/yyyy",
        weekStart: 0,
        todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		minView: 2,
		forceParse: 0,
		pickerPosition:'bottom-left',
		daysOfWeekDisabled: "0,6",
    });
    $(function(){
        $('.form_date3 input,.form_date4 input').change(function(){
            var strEndDate=$('.form_date4 input').val();
            var strStartDate=$('.form_date3 input').val();
            $('.form_date3').datetimepicker('setEndDate', strEndDate);
            $('.form_date4').datetimepicker('setStartDate', strStartDate);
         });
    });
    function showActivity()
	{
		var xmlhttp;
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if(xmlhttp){
			var centreName = $("#ddlCentre").find("option:selected").text();
			xmlhttp.open("GET","./Reports/dropdownList.php?q="+centreName,true);
			xmlhttp.onreadystatechange=function(){
				if(this.readyState ==4 && xmlhttp.status==200){
					var data = JSON.parse(xmlhttp.responseText);
					$('#activity')[0].options.length = 1;
					for(var i=0;i<data.length;i++) 
					{	
						var opt = document.createElement('option');				
						opt.value=data[i].activityId;
						opt.innerHTML = data[i].activityName;
						document.getElementById("activity").appendChild(opt);
					}
				}
			}
			xmlhttp.send();
		}	
	}	
</script>
</html>

